﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：UserReg.ascx.cs
//
//
//文件功能描述：顾客注册....
//
//
//创建标识：2007.11.19 李正平
//创建描述：
//
//
//修改标识:2007.11.29 李正平
//修改描述:用MultiView替换了Panel,将以前用的TextBox输入省份和市都用了下拉列表,对注释进行了修改
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Threading;
using System.IO;
using System.Xml;
using System.Text;

using JXMSTC.SmartBusiness.Common.Model;

public partial class Commom_UserReg : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

        
        if (!IsPostBack)
        {
            //
            //绑定省份
            //
            LoadXMLProvince();

            //
            //将信息提示 设置为空
            //
            this.lblMessage.Text = string.Empty;
        }
    }


    /// <summary>
    /// 从XML文件中绑定出省市
    /// </summary>
    protected void LoadXMLProvince()
    {
        //
        //文件名
        //
        string dataFile = "~/XML/CartInfo/ProvinceList.xml";
        StreamReader sdrXML = new StreamReader(Server.MapPath(dataFile));
        XmlDataDocument dataDoc = new XmlDataDocument();
        
        //
        //创建该对象为了读取XML
        //
        dataDoc.DataSet.ReadXml(sdrXML);

        //
        //读取XML文件内容
        //
        dropProvince.DataSource = dataDoc.DataSet.Tables[0].DefaultView;
        dropProvince.DataTextField = "ProvinceName";
        dropProvince.DataValueField = "ProvinceID";
        dropProvince.DataBind();

    }


    /// <summary>
    /// 同意协议
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAgree_Click(object sender, EventArgs e)
    {
        //
        //同意协议,进入下一页
        //        
        mtvRegesiter.ActiveViewIndex++;
    }


    /// <summary>
    /// 不同意协议
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisAgree_Click(object sender, EventArgs e)
    {

        //
        //不同意则不允许注册,跳转到首页
        //
        Server.Transfer("~/Default.aspx");

    }


    /// <summary>
    /// 对数据进行加密
    /// </summary>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    protected byte[] GetPassword(string strPassword)
    {
        //
        //转化为二进制数组
        //
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] arSource = UE.GetBytes(strPassword);

        //
        //哈希加密
        //
        SHA1Managed SHharsh= new SHA1Managed();

        //
        //长度为20位
        //
        byte[] arHarshValue = new byte[20];

        arHarshValue = SHharsh.ComputeHash(arSource);

        //
        //返回一个二进制数组
        //
        return arHarshValue;
    }


    /// <summary>
    /// 提交顾客注册信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        //
        //声明一个BOOL类型变量,看用户名是否存在
        //
        bool blnCustomerAccountExist = true;

        localhost.CustomerService customerAccountExist = new localhost.CustomerService();

        //
        //判断顾客注册的登录名是否存在
        //
        blnCustomerAccountExist = customerAccountExist.CustomerAccountIsExist(this.txtUserName.Text.Trim());
       

        if (blnCustomerAccountExist)
        {
            //
            //该登录名存在
            //
            this.lblMessage.Text = "此用户名已存在,请重新输入!";
        }
        else
        {
            //
            //该登录名不存在,则可以注册
            //

            localhost.Customer customer = new localhost.Customer();
           
            //
            //填写注册信息
            //           
            customer.Account = txtUserName.Text;

            //
            //获取密码
            //
            customer.Password = this.GetPassword(txtPass.Text.Trim());
           
            customer.FirstName = txtFirstName.Text;
          
            customer.LastName = txtLastName.Text;
          
            customer.Email = txtMail.Text;
           
            customer.Provice = dropProvince.SelectedItem.Text;

            customer.City = dropCity.SelectedItem.Text;
            
            customer.Address = txtAddress.Text;
            
            customer.Phone = txtPhone.Text;
            
            customer.Zip = txtZip.Text;


               
            //
            //顾客注册信息,保存到数据库
            //
            localhost.CustomerService customerRegesiter = new localhost.CustomerService();
            customerRegesiter.CustomerRegisterCompleted += new localhost.CustomerRegisterCompletedEventHandler(ws_CustomerRegisterCompleted);
            
            //
            //返回调用结果
            //
            customerRegesiter.CustomerRegisterAsync(customer);
            
        }

    }


    /// <summary>
    /// 返回注册结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void ws_CustomerRegisterCompleted(object sender, localhost.CustomerRegisterCompletedEventArgs e)
    {

        //
        //注册操作是否成功返回值
        //
        bool success = e.Result;
            
        if (success)
        {
            
            //
            //注册成功
            //
            Server.Transfer("~/MemberCenter/UserLogin.aspx");
        }
        else
        {
            //
            //注册失败
            //
            this.lblMessage.Text = "抱歉,注册失败！请检查并重新输入！";
                
        }        
    }


    /// <summary>
    /// 绑定市
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dropProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        //
        //获得选择的省份
        //
        string strProvince = dropProvince.SelectedValue;

        //
        //获得所选省份的市
        //
        string strXMLCity = "~/XML/CartInfo/CityList.xml";
        StreamReader sdrXMLCity = new StreamReader(Server.MapPath(strXMLCity));
        XmlDataDocument dataDoc = new XmlDataDocument();
        dataDoc.DataSet.ReadXml(sdrXMLCity);

        //
        //过滤条件
        //
        DataView dvCity = dataDoc.DataSet.Tables[0].DefaultView;

        dvCity.RowFilter="ProvinceID=" + strProvince;

        //
        //绑定输出
        //
        dropCity.DataSource = dvCity;
        dropCity.DataTextField = "CityName";
        dropCity.DataBind();
    }


    /// <summary>
    /// 取消注册
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        //
        //置为空
        //
        txtAddress.Text = string.Empty;
        txtConPass.Text = string.Empty;
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtMail.Text = string.Empty;
        txtPass.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtUserName.Text = string.Empty;
        txtZip.Text = string.Empty;
        //dropProvince.SelectedIndex = 0;
        //dropCity.SelectedIndex = 0;
    }
}
